<?php
namespace plugins\goods\model;

use think\Db;
use think\Model;
use plugins\goods\model\GoodsModel;

//规格的工具模型？大概
class GoodsSpecModel extends Model
{
    public $list;
    public $info;
    //设置规格主页用的list
    public function setlist($list)
    {
        foreach ($list as $key => &$value) {
            $info = db('goods_spec_name')->field('spec_name')->where(['spec_id' => $value['spec_id']])->select()->toArray();
            //得到规格名称
            $str = '';
            foreach ($info as $k => $v) {
                $str .= $v['spec_name'] . "、";
            }
            $str           = substr($str, 0, strlen($str) - 3);
            $value['name'] = $str;
            //得到所属分类
            // $catstr = '';
            // $catstr = $this->getCatStr($value['cat_id'],$catstr);
            // $catstr = substr($catstr,0,strlen($catstr)-6);
            // $value['catstr'] = $catstr;
        }
        $this->list = $list;
    }
    //拼接分类
    // private function getCatStr($catid,$catstr)
    // {
    //  $cat = db('goods_cats')->field('pid,catname')->where(['catid'=>$catid])->find();
    //  if(!empty($cat)){
    //      $catstr = $cat['catname'].'——'.$catstr;
    //      if($cat['pid'] != '0')
    //      {
    //          $catstr = $this->getCatStr($cat['pid'],$catstr);
    //      }
    //  }
    //  return $catstr;
    // }
    //传入单个规格id获得规格详情
    public function setinfo($spec_id)
    {
        $this->info = db('goods_spec')->where(['spec_id' => $spec_id])->find();
        $name       = db('goods_spec_name')->where(['spec_id' => $spec_id])->select()->toArray();
        foreach ($name as $key => &$value) {
            $attr          = db('goods_spec_attr')->where(['name_id' => $value['name_id']])->select()->toArray();
            $value['attr'] = $attr;
        }
        $this->info['name'] = $name;
    }


    /**
     * 判断规格是否可以删除
     */
    public function affirmDelete($spec_id = [])
    {
        //判断是否存在商品使用 + 是否已上架 + 是否正在审核中  
        //如存在则提示不可删除。
        $goods_model = new GoodsModel;
        $goods_list = $goods_model->where('spec_temp', 'in', $spec_id)->where('status', 1)->where('audit_status','in', [1,2])->select()->toArray();
        

        $da = [
            'goods_count' => 0,
            'goods_ids'   => [],
        ];
        if (empty($goods_list)) {
            return $da;
        }
        $da['goods_count'] = count($goods_list);
        $da['goods_ids']   = array_column($goods_list, 'id');

        return $da;
    }





}
